package com.ruoyi.mining.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.mining.dto.BaseDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mining.domain.Student;
import com.ruoyi.mining.service.IStudentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 学生信息Controller
 *
 * @author liyuxi
 * @date 2024-04-19
 */
@Api(value = "学生信息", tags = "学生信息Controller")
@RestController
@RequestMapping("/student")
public class StudentController extends BaseController {
    @Autowired
    private IStudentService studentService;

    /**
     * 查询学生信息列表
     */
//    @RequiresPermissions("mining:student:list")
    @GetMapping("/list")
    public TableDataInfo list(BaseDto student) {
        Student student1 = new Student();
        student1.setName(student.getName());
        startPage();
        List<Student> list = studentService.selectStudentList(student1);
        return getDataTable(list);
    }


    /**
     * 获取学生信息详细信息
     */
//    @RequiresPermissions("mining:student:query")
    @ApiOperation("获取学生信息详细信息，可以传userid，手机号，学生id")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(studentService.selectStudentById(id));
    }


    /**
     * 修改学生信息
     */
//    @RequiresPermissions("mining:student:edit")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Student student) {
        try {
            studentService.updateStudent(student);
            return success(student);
        }catch ( Exception e){
            return error("保存学生错误");
        }

    }

    /**
     * 删除学生信息
     */
//    @RequiresPermissions("mining:student:remove")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(studentService.deleteStudentByIds(ids));
    }


    @ApiOperation("通过手机号码或者userid查询学生信息")
    @GetMapping("/queryStudentByIdOrPhone")
    public AjaxResult queryStudentByIdOrPhone(@RequestParam @Validated @NotBlank(message = "userId不能为空") String userId) {


        if (StringUtils.isEmpty(userId)) {
            return error("userId号码不对");
        }
        Student student = studentService.queryStudentByIdOrPhone(userId);

        if (student == null) {
            return error("该学生不存在");
        }
        return success(student);
    }
}
